package com.sojoys.artifact.tools;

/**
 * 
 * @author win7
 * 
 */
public class ToolIP {

	/**
	 * 将字符串型ip转成int型ip
	 * 
	 * @param strIp
	 * @return
	 */
	public static int Ip2Int(String strIp) {
		String[] ss = strIp.split("\\.");
		if (ss.length != 4) {
			return 0;
		}
		byte[] bytes = new byte[ss.length];
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] = (byte) Integer.parseInt(ss[i]);
		}
		return byte2Int(bytes);
	}

	/**
	 * 将int型ip转成String型ip
	 * 
	 * @param intIp
	 * @return
	 */
	public static String int2Ip(int intIp) {
		byte[] bytes = int2byte(intIp);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < 4; i++) {
			sb.append(bytes[i] & 0xFF);
			if (i < 3) {
				sb.append(".");
			}
		}
		return sb.toString();
	}

	private static byte[] int2byte(int i) {
		byte[] bytes = new byte[4];
		bytes[0] = (byte) (0xff & i);
		bytes[1] = (byte) ((0xff00 & i) >> 8);
		bytes[2] = (byte) ((0xff0000 & i) >> 16);
		bytes[3] = (byte) ((0xff000000 & i) >> 24);
		return bytes;
	}

	private static int byte2Int(byte[] bytes) {
		int n = bytes[0] & 0xFF;
		n |= ((bytes[1] << 8) & 0xFF00);
		n |= ((bytes[2] << 16) & 0xFF0000);
		n |= ((bytes[3] << 24) & 0xFF000000);
		return n;
	}

	// /**
	// * 通过HttpServletRequest返回IP地址
	// *
	// * @param request
	// * HttpServletRequest
	// * @return ip String
	// * @throws Exception
	// */
	// public static String getIpAddr(HttpServletRequest request) {
	// String ip = request.getHeader("x-forwarded-for");
	// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	// ip = request.getHeader("Proxy-Client-IP");
	// }
	// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	// ip = request.getHeader("WL-Proxy-Client-IP");
	// }
	// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	// ip = request.getHeader("HTTP_CLIENT_IP");
	// }
	// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	// ip = request.getHeader("HTTP_X_FORWARDED_FOR");
	// }
	// if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	// ip = request.getRemoteAddr();
	// }
	// return ip;
	// }

	/**
	 * 是否在CIDR规则配置范围内
	 * 
	 * @param ip
	 * @param cidr
	 * @return
	 */
	public static boolean isInRange(String ip, String cidr) {
		String[] ips = ip.split("\\.");
		int ipAddr = (Integer.parseInt(ips[0]) << 24) | (Integer.parseInt(ips[1]) << 16)
				| (Integer.parseInt(ips[2]) << 8) | Integer.parseInt(ips[3]);
		int type = Integer.parseInt(cidr.replaceAll(".*/", ""));
		int mask = 0xFFFFFFFF << (32 - type);
		String cidrIp = cidr.replaceAll("/.*", "");
		String[] cidrIps = cidrIp.split("\\.");
		int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24) | (Integer.parseInt(cidrIps[1]) << 16)
				| (Integer.parseInt(cidrIps[2]) << 8) | Integer.parseInt(cidrIps[3]);

		return (ipAddr & mask) == (cidrIpAddr & mask);
	}

	/**
	 * 是否为本地IP地址
	 * 
	 * @param ip
	 * @return
	 */
	public static boolean isLocal(String ip) {
		if (ip.equals("0:0:0:0:0:0:0:1")) {
			return true;
		}
		if (ip.equals("0.0.0.1")) {
			return true;
		}
		if (ip.equals("127.0.0.1")) {
			return true;
		}
		if (ip.equals("localhost")) {
			return true;
		}
		return false;
	}

	// /**
	//
	// * 通过IP获得地址信息，调用的是淘宝的IP接口
	//
	// *
	//
	// * @param ip
	//
	// * @return
	//
	// */
	// final static String addr = "http://ip.taobao.com/service/getIpInfo.php";
	// public static String getAddr(String ip) {
	// String info = "";
	// String result = ToolSimpleHttp.sendGet(addr, "ip="+ip);
	// Map obj = JsonManager.me().getJson().parse(result, Map.class);
	// return "";
	// }

	// public static void main(String[] args) {
	//// String ip1 = "192.168.0.1";
	//// int intIp = Ip2Int(ip1);
	//// String ip2 = int2Ip(intIp);
	//// System.out.println(ip2.equals(ip1));
	//
	// String result = ToolSimpleHttp.sendGet(addr, "ip="+"125.70.179.42");
	// System.out.println(result);
	// }
}